// pages/user/user.js
import { Base } from '../../utils/base.js'
import { Login } from './login-model.js'
const app = getApp();
let base = new Base();
let login = new Login();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    disabled: false,
    yzm: '',
    phone: '',
    timer: 59,
    isShow: false
  },
  onLoad(options) {

    let sessionId = wx.getStorageSync("sessionId");
   

    if (sessionId != "") {
      console.log('登录状态-----------',options);
      if (options && options.type == "product") {
        wx.navigateTo({
          url: '/pages/product/product?id=' + options.id,
        })

      } else {
        wx.switchTab({
          url: '/pages/home/home'
        })

      }

    } else {
        app.getUserSetting()        
      this.setData({
        isShow: true
      })
      wx.setNavigationBarTitle({
          title: '登录'
      })
      wx.setNavigationBarColor({
          frontColor: '#ffffff',
          backgroundColor: '#000000',
          
      })
    }
  },
  onShow(){
    wx.getSetting({})
  },
 
  setPhone(e) {
    let val = e.detail.value
    this.setData({
      phone: val
    })
  },
  checkPhone(e) {
    console.log(e)
    let val = e.detail.value

    if (val == '') {
      wx.showToast({
          icon: 'none',
          title: '请输入手机号'
      })
    } else if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(val)) {
      wx.showToast({
          icon: 'none',
          title: '请输入正确的手机号'
      })
    } 
  },
  getUserInfo(e){
    console.log(e)
    wx.getSetting({
        success(res){
            console.log(res,'res')
        }
    })
    let { phone } = this.data
    console.log('app.globalData.isLogin', app.globalData.isLogin)
     if (e.detail.errMsg == 'getUserInfo:fail auth deny') {
        console.log('fail')
        app.showTips(() => {
            this.checkPhoneGetYzm(()=>{
                this.getYzm()
            })
        })
     }else{
        if (app.globalData.isLogin){
            this.checkPhoneGetYzm(() => {
                this.getYzm()
            })
        }else{
            if (phone != ''){
                wx.showLoading({
                    title: '发送中',
                })
            }
            console.log('app.globalData.isLogin', app.globalData.isLogin)            
            app._loginUser(e.detail, () => {
                this.checkPhoneGetYzm(() => {
                    this.getYzm()
                })
            })

        }
     }
  },
  checkPhoneGetYzm(cb){
      let { phone } = this.data
      if(phone == '') {
          wx.showToast({
              icon: 'none',
              title: '请输入手机号'
          })
      } else if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(phone)) {
    wx.showToast({
        icon: 'none',
        title: '请输入正确的手机号'
    })
}else{
         cb && cb()
}
  },
  getYzm() {
    console.log(this.data.phone)
    let { phone } = this.data
    let timer = null
    let time = 59
    console.log(/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(phone), phone.length)
    console.log('send')
    login.checkVerifPhone({ verifyPhone: phone }, res => {
        console.log('code',res)
        if (!(res.status && res.code == 'usable')) {
            wx.showToast({
                icon: 'none',
                title: res.message
            })
        } else if (res.code == 'usable'){
            login.sendVerifyCode({ verifyPhone: phone }, res => {
                wx.hideLoading()
                if (!res.status) {
                    wx.showToast({
                        icon: 'none',
                        title: res.message
                    })
                } else {
                    wx.showToast({
                        icon: 'none',
                        title: '发送验证码成功，请您查收'
                    })
                    this.setData({
                        disabled: true
                    })
                    timer = setInterval(() => {
                        if (time == 0) {
                            clearInterval(timer)
                            this.setData({
                                disabled: false,
                                timer: 59
                            })
                        }
                        this.setData({
                            timer: time
                        })
                        time--
                    }, 1000)
                }
            })

        }
    })
  },
  onLogin: function (e) {
    let that = this;
    let name = e.detail.value.name;
    let yzm = e.detail.value.yzm;
    let yqm = e.detail.value.yqm;
    if (name == "") {
      wx.showToast({
          icon: 'none',
          title: '手机号不能为空'
      })
      return;
    }
    if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(name)) {
      wx.showToast({
          icon: 'none',
          title: '请输入正确的手机号'
      })
      return;
    }
    if (yzm.trim() == "") {
      wx.showToast({
          icon: 'none',
          title: '验证码不能为空'
      })
      return;
    }
    if (!/\d{6}/g.test(yzm)) {
      wx.showToast({
          icon: 'none',
          title: '验证码为6位的数字'
      })
      return;
    }
    if ( yqm.trim() == "") {
        wx.showToast({
            icon: 'none',
            title: '邀请码不能为空'
        })
        return;
    }
    if (!/^\w{6}$/g.test(yqm)) {
        wx.showToast({
            icon: 'none',
            title: '邀请码为6位'
        })
        return;
    }
    let params = {
      "verifyPhone": name,
      "verifyCode": yzm,
      "inviteCode":yqm
    }
    console.log(params)
    login.registerUser(params, res => {
      console.log(res);
      if (res.status && res.code == 'success') {
        wx.setStorageSync('sessionId', res.data.sessionId)
        wx.showToast({
          title: '登录成功',
          icon: 'success',
          duration: 1500,
          success() {
            setTimeout(() => {
              wx.switchTab({
                url: '/pages/home/home',
                success: function (e) {
                  var page = getCurrentPages().pop();
                  if (page == undefined || page == null) return;
                  page.onLoad();

                }
              })
            }, 500);

          }
        })

      } else {
        wx.showToast({
            icon: 'none',
            title: res.message
        })
      }
    });
  },

  onShow() {

    console.log("onShow-----------------------");

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})